#include <mymuduo/tcpserver.h>
#include <string>
#include <mymuduo/tcpconnection.h>
#include <mymuduo/eventloop.h>
#include <iostream>
class EchoServer{
    public:
    EchoServer(EventLoop *loop,
            const InetAddress &addr, 
            const std::string &name)
        :loop_(loop)
        , server_(new TcpServer(loop, addr, name, TcpServer::kNoReusePort))
        
            {
                server_->setConnectionCallBack(
                    std::bind(&EchoServer::onConnection, this, std::placeholders::_1)
                );
                server_->setMessageCallBack(
                    std::bind(&EchoServer::onMessage, this, std::placeholders::_1,std::placeholders::_2, std::placeholders::_3)
                );

                server_->setThreadNum(3);

            }
    
    void start(){
        server_->start();
    }
    private:
        void onConnection(const TcpConnectionPtr& conn){
            if(conn->connected()){
                std::cout<<"conn UP " << conn->peerAddress().toIpPort()<<std::endl; 
            }
        }

        void onMessage(const TcpConnectionPtr& conn,
                                                Buffer* buff, TimeStamp time)
        {
            std::string msg = buff->retriveAllAsString();
            conn->send(msg);
            conn->shutdown();
        }
    private:
        EventLoop* loop_;
        TcpServer* server_;     
};

int main(){
    EventLoop eventLoop;
    InetAddress addr(8080);
    EchoServer echoServer(&eventLoop, addr, "echo server");
    echoServer.start();
    eventLoop.loop();
}